package com.redsoil.house.ui.news.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.redsoil.house.R;
import com.redsoil.house.base.RootActivity;
import com.redsoil.house.ui.matter.activity.MatterActivity;
import com.redsoil.house.ui.matter.contract.MatterContract;
import com.redsoil.house.ui.matter.presenter.MatterPresenter;
import com.redsoil.house.ui.news.contract.NewsInfoContract;
import com.redsoil.house.ui.news.presenter.NewsInfoPresenter;
import com.redsoil.house.widget.actionbar.XBaseTopBar;
import com.redsoil.house.widget.actionbar.XBaseTopBarImp;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 咨询详情页面
 */
public class NewsInfoActivity extends RootActivity<NewsInfoPresenter> implements NewsInfoContract.View,XBaseTopBarImp{

    @BindView(R.id.webView)
    WebView webView;

//    @BindView(R.id.topbar_back_img)
//    ImageView topbar_back_img;

    private WebSettings mWebSettings;

    private String url;

    @BindView(R.id.main_topbar)
    XBaseTopBar main_topbar;

    @Override
    protected int getLayout() {
        return R.layout.activity_news_info;
    }

    @Override
    protected void initEventAndData() {

        main_topbar.setxBaseTopBarImp(this);

        url = getIntent().getStringExtra("url");

        mWebSettings = webView.getSettings();
        mWebSettings.setJavaScriptEnabled(true);
        webView.loadUrl(url);


        //设置不用系统浏览器打开,直接显示在当前Webview
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        //设置WebChromeClient类
        webView.setWebChromeClient(new WebChromeClient() {


            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
//                System.out.println("标题在这里");
//                mtitle.setText(title);
            }


            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
//                if (newProgress < 100) {
//                    String progress = newProgress + "%";
//                    loading.setText(progress);
//                } else if (newProgress == 100) {
//                    String progress = newProgress + "%";
//                    loading.setText(progress);
//                }
            }
        });

        //设置WebViewClient类
        webView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                System.out.println("开始加载了");
//                beginLoading.setText("开始加载了");

            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
//                endLoading.setText("结束加载了");

            }
        });
    }


    @Override
    public void left() {
        finish();
    }

    @Override
    public void right() {

    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

//    @OnClick(R.id.topbar_back_img)
//    void onBack(){
//        finish();
//    }

    /**
     * 打开新Activity
     *
     * @param context
     * @return
     */
    public static Intent newInstance(Context context,String url) {
        Intent intent = new Intent(context, NewsInfoActivity.class);
        intent.putExtra("url",url);
        return intent;
    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();

            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }

}
